/*个人信息*/
function myselfinfo() {
    layer.open({
        type: 1,
        area: ['300px', '200px'],
        fix: false, //不固定
        maxmin: true,
        shade: 0.4,
        title: '查看信息',
        content: '<div>管理员信息</div>',
    })
}

function modifyPwd() {
    window.location.href = root.sso.modifyPwdUrl
}
/*资讯-添加*/
function Signout(title, url) {
    layer.confirm(title, function(index) {
        window.location.href = root.sso.url
            /*window.localStorage.removeItem('loginData');*/
        delCookie()
    })
}
/*资讯-添加*/
function article_add(title, url) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url,
    })
    layer.full(index)
}
/*图片-添加*/
function picture_add(title, url) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url,
    })
    layer.full(index)
}
/*产品-添加*/
function product_add(title, url) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url,
    })
    layer.full(index)
}
/*用户-添加*/
function member_add(title, url, w, h) {
    layer_show(title, url, w, h)
}

function getCookie(sName) {
    var aCookie = document.cookie.split('; ')
    for (var i = 0; i < aCookie.length; i++) {
        var aCrumb = aCookie[i].split('=')
        if (sName == aCrumb[0]) return decodeURIComponent(aCrumb[1])
    }
    return null
}

function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
    var r = window.location.search.substr(1).match(reg)
    if (r != null) return unescape(r[2])
    return null
}

var mtkname = getCookie('m_tk') /*sName为要取的key名*/
if (mtkname == null) {
    mtkname = getQueryString('m_tk')
}

//本地调用
 var loginDatauser = '%7B%22tokenID%22%3A%22eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MTM5NTg0NDAzNDAsInBheWxvYWQiOiJ7XCJkZXBhcnRtZW50XCI6bnVsbCxcInJvbGVcIjpudWxsLFwicGFnZVwiOntcInNob3dDb3VudFwiOjUwMDAsXCJ0b3RhbFBhZ2VcIjowLFwidG90YWxSZXN1bHRcIjowLFwiY3VycmVudFBhZ2VcIjowLFwiY3VycmVudFJlc3VsdFwiOjAsXCJlbnRpdHlPckZpZWxkXCI6ZmFsc2UsXCJwYWdlU3RyXCI6XCJcIixcInBkXCI6e30sXCJ1c2VyX0lEXCI6MCxcImRlcGFydG1lbnRfSURcIjowfSxcInJvbGVMaXN0XCI6bnVsbCxcInN0YXR1c1wiOlwiMFwiLFwidXNlcl9JRFwiOjg2LFwidXNlcm5hbWVcIjpcInpoYW5neXVcIixcIm5hbWVcIjpcIuW8oOeRnFwiLFwiZGVwYXJ0bWVudF9JRFwiOjEsXCJlbWFpbFwiOm51bGwsXCJwaG9uZVwiOm51bGwsXCJlbnRyeV9USU1FXCI6bnVsbCxcImNyZWF0X1RJTUVcIjpudWxsLFwibnVtYmVyXCI6XCI5MDAwMDM2XCIsXCJlblwiOm51bGwsXCJwYXNzd29yZFwiOm51bGwsXCJyaWdodHNcIjpudWxsLFwiYmlhbm1hXCI6bnVsbH0ifQ.TKag4zvrXhjKyM6cu7N4zW_3u17qrswcO1VUR0hHYAc%22%2C%22roleList%22%3A%5B32%5D%2C%22department%22%3A%7B%22target%22%3Anull%2C%22department%22%3Anull%2C%22subDepartment%22%3Anull%2C%22hasDepartment%22%3Afalse%2C%22treeurl%22%3Anull%2C%22parent_ID%22%3A0%2C%22department_NAME%22%3A%22%E5%8C%97%E4%BA%AC%E9%91%AB%E6%96%B9%E7%9B%9B%E4%BA%94%E9%87%91%E4%BA%A4%E7%94%B5%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8%22%2C%22p_BIANMA%22%3Anull%2C%22bianma%22%3Anull%2C%22max_NUM%22%3Anull%2C%22department_ID%22%3A1%7D%2C%22user%22%3A%7B%22department%22%3Anull%2C%22role%22%3Anull%2C%22page%22%3A%7B%22showCount%22%3A5000%2C%22totalPage%22%3A0%2C%22totalResult%22%3A0%2C%22currentPage%22%3A0%2C%22currentResult%22%3A0%2C%22entityOrField%22%3Afalse%2C%22pageStr%22%3A%22%22%2C%22pd%22%3A%7B%7D%2C%22department_ID%22%3A0%2C%22user_ID%22%3A0%7D%2C%22roleList%22%3Anull%2C%22rights%22%3Anull%2C%22bianma%22%3Anull%2C%22department_ID%22%3A1%2C%22user_ID%22%3A86%2C%22username%22%3A%22zhangyu%22%2C%22name%22%3A%22%E5%BC%A0%E7%91%9C%22%2C%22number%22%3A%229000036%22%2C%22password%22%3Anull%2C%22phone%22%3Anull%2C%22email%22%3Anull%2C%22entry_TIME%22%3Anull%2C%22status%22%3A%220%22%2C%22creat_TIME%22%3Anull%2C%22en%22%3Anull%7D%7D'
 web.cookie('loginData', loginDatauser);
 web.cookie('token', hex_md5(JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID + 'xfs7Alc74oCVpHXg97etTs'))
 web.cookie('memberId', JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID)


//环境调用
var loginData = JSON.parse(web.readCookie('loginData'))
console.log(loginData)
if (loginData == null) {
    if (mtkname == null) {
        window.location.href = root.sso.url
    } else {
        // 去验证cokkies的正确性并获取用户名及用户的权限
        $.ajax({
            type: 'post',
            url: root.accessInter.login,
            data: { mTk: mtkname },
            dataType: 'json',
            success: function(data) {
                if (data.code == '200') {
                    var loginData = data.data
                    window.session_storage.setItem('token', data.data.tokenID)
                    web.cookie(
                        'loginData',
                        encodeURIComponent(JSON.stringify(loginData))
                    )
                    web.cookie(
                        'adminToken',
                        JSON.parse(
                            decodeURIComponent(web.readCookie('loginData'))
                        ).adminToken
                    )
                    web.cookie(
                        'adminMemberId',
                        JSON.parse(
                            decodeURIComponent(web.readCookie('loginData'))
                        ).user.user_ID
                    )

                    // web.cookie('token', hex_md5(JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID +'xfs7Alc74oCVpHXg97etTs'))
                    // web.cookie('memberId',JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID)
                    /*window.localStorage.setItem('loginData',JSON.stringify(loginData))*/
                    document.cookie =
                        'user_id=' + data.data.user.user_ID + ';path=/'
                    setTimeout(function() {
                        window.location.href = 'index.html'
                    }, 200)
                } else {
                    window.location.href = root.sso.url
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest)
            },
        })
    }
} else {
    $('.calss_name').text(loginData.user.username)
    $('.kehu_name').text(loginData.user.name)
}
if (loginData != null) {
    var rolelist = loginData.roleList
    var roles = ''
    if (rolelist != null) {
        roles = rolelist.join(',')
    }
}

/*for(var i = 0;i<rolelist.length;i++)
{
    if(i+1<rolelist.length){
        roles =rolelist[i]+roles;
    }
    else{
        roles =rolelist[i]+','+roles;
    }

}*/
if (roles == 'null,') {
    var dataoutput = {
        username: loginData.user.username,
    }
} else {
    var dataoutput = {
        username: loginData.user.username,
        roleList: roles,
    }
}
$.ajax({
    type: 'POST',
    url: root.accessInter.access_main_api,
    cache: false, //禁用缓存
    dataType: 'json',
    data: dataoutput,
    success: function(data) {
        window.localStorage.setItem(
            'roleMenuList',
            JSON.stringify(data.roleMenuList)
        )
        getMenuList(data.FirstMenuMap.menu_id)

        //控制事项提示显示隐藏
        for (var j = 0; j < data.roleMenuList.length; j++) {
            var ppp = data.roleMenuList[j].button_id
            var lk = ppp.split(',')
            for (var i = 0; i < lk.length; i++) {
                if (lk[i] == 49) {
                    //普通客户认证
                    $('#hide1').css({
                        display: 'block',
                    })
                } else if (lk[i] == 19) {
                    //电子券管理
                    $('#hide9').css({
                        display: 'block',
                    })
                } else if (lk[i] == 32) {
                    //我要合作
                    $('#hide10').css({
                        display: 'block',
                    })
                    $('#hideaic').css({
                        display: 'block',
                    })

                } else if (lk[i] == 534) {
                    //未生成订单
                    $('#hide11').css({
                        display: 'block',
                    })
                } else if (lk[i] == 563) {
                    //意见反馈
                    $('#hide12').css({
                        display: 'block',
                    })
                }
                if (lk[i] == 8) {
                    //客户管理客户池
                    $("#customerpool").css({
                        display: "block"
                    });
                }
                //云采加对接的模块

                //异常订单

                if (lk[i] == 11 || lk[i] == 504) {
                    $('#hide2').css({
                        display: 'block',
                    })
                }
                /*云采*/
                if (lk[i] == 11) {
                    $('#Yuncaiabnormal').css({
                        display: 'block',
                    })
                }
                /*对接*/
                if (lk[i] == 536) {
                    $('#hide15').css({
                        display: 'block',
                    })
                }
                if (lk[i] == 536) {
                    $('#Duijeiabnormal').css({
                        display: 'block',
                    })
                }
                //询价单管理
                if (lk[i] == 31 || lk[i] == 535) {
                    $('#hide3').css({
                        display: 'block',
                    })
                }

                /*云采*/
                if (lk[i] == 31) {
                    $('.Yuncaiinquiry').css({
                        display: 'block',
                    })
                }
                /*对接*/
                if (lk[i] == 535) {
                    $('#hide13').css({
                        display: 'block',
                    })
                }
                //取消订单申请管理
                if (lk[i] == 48 || lk[i] == 598) {
                    $('#hide5').css({
                        display: 'block',
                    })
                }

                /*云采*/
                if (lk[i] == 48) {
                    $('.Yuncaicancel').css({
                        display: 'block',
                    })
                }
                /*对接*/
                if (lk[i] == 598) {
                    $('#hide14').css({
                        display: 'block',
                    })
                }
                //补开发票管理
                if (lk[i] == 51) {
                    $('#hide6').css({
                        display: 'block',
                    })
                }
                //退货管理
                if (lk[i] == 12 || lk[i] == 511) {
                    $('#hide7').css({
                        display: 'block',
                    })
                }

                /*云采*/
                if (lk[i] == 12) {
                    $('#YuncaireturnGoods').css({
                        display: 'block',
                    })
                }
                /*对接*/
                if (lk[i] == 511) {
                    $('#DuijeireturnGoods').css({
                        display: 'block',
                    })
                }
                //维修管理

                if (lk[i] == 12 || lk[i] == 512) {
                    $('#hide8').css({
                        display: 'block',
                    })
                }

                /*云采*/
                if (lk[i] == 13) {
                    $('#Yuncairepair').css({
                        display: 'block',
                    })
                }
                /*对接*/
                if (lk[i] == 512) {
                    $('#Duijeirepair').css({
                        display: 'block',
                    })
                }
                if (lk[i] == 250) {
                    $('#hide17').css({
                        display: 'block',
                    })
                    $('#hide18').css({
                        display: 'block',
                    })
                }

                /*积分订单*/
                if (lk[i] == 561) {
                    $('#hide16').css({
                        display: 'block',
                    })
                }
            }
        }
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {},
})

//渲染菜单
function getMenuList(mels) {
    var data = mels.split(',')
    for (var i in data) {
        $('#' + data[i] + '').removeClass('hide')
    }
}